package com.taotao.controller;

import com.taotao.pojo.Item;
import com.taotao.pojo.ItemDesc;
import com.taotao.service.ItemDescService;
import com.taotao.service.ItemService;
import com.taotao.util.ExceptionUtil;
import com.taotao.util.TaotaoResult;
import com.taotao.util.model.EasyUIResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.sun.tools.javac.jvm.ByteCodes.ret;

/**
 * @author hong.zheng
 * @Description
 * @date 2018-08-22-10:35
 **/
@Controller
@RequestMapping("/item")
public class ItemController {
    private final  Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemDescService itemDescService;

    @RequestMapping("/list")
    //设置相应的内容为json数据
    @ResponseBody
    public EasyUIResult getItemlist(@RequestParam(defaultValue="1")Integer page,
                                    @RequestParam(defaultValue="30")Integer rows) throws Exception {
        //查询商品列表
        EasyUIResult result = itemService.getItemList(page, rows);
        return result;
    }
    @RequestMapping("/save")
    @ResponseBody
    public TaotaoResult addItem(Item item, String desc,String itemParams) {
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemDesc(desc);
        TaotaoResult result = itemService.addItem(item, itemDesc,itemParams);
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public TaotaoResult editItem(Item item, String desc,String itemParams) {

        try {
            itemService.updateItem(item, desc,itemParams);
            return TaotaoResult.ok();
        }catch (Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
    }
    @RequestMapping("/findItemById")
    @ResponseBody
    public TaotaoResult getItemByItemId(Long itemId) {
        Item item = itemService.getItemByItemId(itemId);
        TaotaoResult taotaoResult = new TaotaoResult(item);
        return taotaoResult;
    }
    @RequestMapping("/findItemDescById")
    @ResponseBody
    public TaotaoResult getItemDescByItemId(Long itemId) {
        ItemDesc itemDesc = itemDescService.getItemDescByItemId(itemId);
        TaotaoResult taotaoResult = new TaotaoResult(itemDesc);
        return taotaoResult;
    }
}
